﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Passbook.Generator;
using Passbook.Generator.Fields;
using System.IO;
using Passbook.Sample.Web.SampleRequests;

namespace Passbook.Sample.Web.Controllers
{
    public class PassController : Controller
    {
        private void SetUpCertificate(PassGeneratorRequest request)
        {
            //request.CertThumbprint = ConfigurationManager.AppSettings["PassBookCertificateThumbprint"];
            request.CertThumbprint = "5c6901d6eb978f00b9a118fba0d87421a13183d0";
            request.CertLocation = System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine;
        }

        private void SetUpPassIdentity(PassGeneratorRequest request)
        {
            request.Identifier = "pass.passbookviet.test";
            request.TeamIdentifier = "FNQFNRY6FQ";
        }

        private void RegisterUpdate(PassGeneratorRequest request)
        {
            request.WebServiceUrl = "http://192.168.1.38:9090/api/";
            //request.WebServiceUrl = "http://bachboss.dyndns.org:9090/api/";
        }

        public ActionResult EventTicket()
        {
            PassGenerator generator = new PassGenerator();

            EventPassGeneratorRequest request = new EventPassGeneratorRequest();
            SetUpCertificate(request);
            SetUpPassIdentity(request);
            request.SerialNumber = "121211";
            request.Description = "My first pass";
            request.OrganizationName = "Tomas McGuinness";
            request.LogoText = "My Pass";
            request.BackgroundColor = "rgb(255,255,255)";
            request.ForegroundColor = "rgb(0,0,0)";

            // override icon and icon retina
            request.Images.Add(PassbookImage.Icon, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/icon.png")));
            request.Images.Add(PassbookImage.IconRetina, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/icon@2x.png")));

            request.EventName = "Jeff Wayne's War of the Worlds";
            request.SeatingSection = 10;
            request.DoorsOpen = new DateTime(2012, 11, 03, 11, 30, 00);

            request.AddBarCode("01927847623423234234", BarcodeType.PKBarcodeFormatPDF417, "UTF-8", "01927847623423234234");

            byte[] generatedPass = generator.Generate(request);
            return new FileContentResult(generatedPass, "application/vnd.apple.pkpass") { FileDownloadName = "Pass.pkpass" };
        }

        public ActionResult BoardingCard()
        {
            PassGenerator generator = new PassGenerator();

            BoardingCardGeneratorRequest request = new BoardingCardGeneratorRequest();
            SetUpCertificate(request);
            SetUpPassIdentity(request);
            request.SerialNumber = "201302171";

            request.Description = "My first pass";
            request.OrganizationName = "Tomas McGuinness";

            request.LogoText = "My Pass";
            request.BackgroundColor = "rgb(255,255,255)";
            request.ForegroundColor = "rgb(0,0,0)";

            // Specific information
            //            
            request.Origin = "San Francisco";
            request.OriginCode = "SFO";

            request.Destination = "London";
            request.DestinationCode = "LDN";

            request.Seat = "7A";
            request.BoardingGate = "F12";
            request.PassengerName = "John Appleseed";

            request.Images.Add(PassbookImage.Icon, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/icon.png")));
            request.Images.Add(PassbookImage.IconRetina, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/icon@2x.png")));
            request.Images.Add(PassbookImage.Logo, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/logo.png")));
            request.Images.Add(PassbookImage.LogoRetina, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/logo@2x.png")));


            request.TransitType = TransitType.PKTransitTypeAir;

            request.AuthenticationToken = "vxwxd7J8AlNNFPS8k0a0FfUFtq0ewzFdc";
            RegisterUpdate(request);


            byte[] generatedPass = generator.Generate(request);

            return new FileContentResult(generatedPass, "application/vnd.apple.pkpass") { FileDownloadName = "Pass.pkpass" };
        }

        public ActionResult Coupon()
        {
            PassGenerator generator = new PassGenerator();

            CouponPassGeneratorRequest request = new CouponPassGeneratorRequest();
            SetUpCertificate(request);
            SetUpPassIdentity(request);
            request.SerialNumber = "121211";
            request.Description = "My first pass";
            request.OrganizationName = "Tomas McGuinness";
            request.LogoText = "My Pass";
            request.BackgroundColor = "rgb(0,0,0)";
            request.ForegroundColor = "rgb(255,255,255)";

            // override icon and icon retina
            request.Images.Add(PassbookImage.Icon, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/icon.png")));
            request.Images.Add(PassbookImage.IconRetina, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/icon@2x.png")));
            request.Images.Add(PassbookImage.Background, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/background.png")));
            request.Images.Add(PassbookImage.BackgroundRetina, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/background@2x.png")));
            request.Images.Add(PassbookImage.Logo, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/logo.png")));
            request.Images.Add(PassbookImage.LogoRetina, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/logo@2x.png")));
            request.Images.Add(PassbookImage.Strip, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/strip.png")));
            request.Images.Add(PassbookImage.StripRetina, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/strip@2x.png")));

            request.AddBarCode("01927847623423234234", BarcodeType.PKBarcodeFormatPDF417, "UTF-8", "01927847623423234234");

            byte[] generatedPass = generator.Generate(request);

            return new FileContentResult(generatedPass, "application/vnd.apple.pkpass") { FileDownloadName = "Pass.pkpass" };
        }

        public ActionResult CafeAction()
        {
            PassGenerator generator = new PassGenerator();

            CouponPassGeneratorRequest request = new CouponPassGeneratorRequest();
            SetUpCertificate(request);
            SetUpPassIdentity(request);

            request.OrganizationName = "Boulder Coffee Co.";
            request.Description = "Boulder Coffee Rewards Card Example";
            request.SerialNumber = "p69f2J";            
            request.Locations.Add(new Location() { Latitude = 37.6189722, Longitude = -122.3748889 });
            request.AddBarCode("12345678", BarcodeType.PKBarcodeFormatPDF417, "UTF-8", "12345678");
            request.LogoText = "Boulder Coffee Co.";
            request.ForegroundColor = "rgb(255,255,255)";
            request.BackgroundColor = "rgb(118,74,50)";
            request.AuxiliaryFields.Add(new StandardField("level", "LEVEL", "Gold"));
            request.AuxiliaryFields.Add(new StandardField("usual", "THE USUAL", "Iced Mocha"));
            request.BackFields.Add(new StandardField("terms", "TERMS AND CONDITIONS",
             "Generico offers this pass, including all information, software, products and services available from this pass or offered as part of or in conjunction with this pass (the \"pass\"), to you, the user, conditioned upon your acceptance of all of the terms, conditions, policies and notices stated here. Generico reserves the right to make changes to these Terms and Conditions immediately by posting the changed Terms and Conditions in this location.\n\nUse the pass at your own risk. This pass is provided to you \"as is,\" without warranty of any kind either express or implied. Neither Generico nor its employees, agents, third-party information providers, merchants, licensors or the like warrant that the pass or its operation will be accurate, reliable, uninterrupted or error-free. No agent or representative has the authority to create any warranty regarding the pass on behalf of Generico. Generico reserves the right to change or discontinue at any time any aspect or feature of the pass."));
            request.PrimaryFields.Add(
                new NumberField("balance", "remaining balance", 25, FieldNumberStyle.Unspecified) { CurrencyCode = "USD" }
            );


            request.Images.Add(PassbookImage.Icon, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/icon.png")));
            request.Images.Add(PassbookImage.IconRetina, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/icon@2x.png")));
            request.Images.Add(PassbookImage.Background, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/background.png")));
            request.Images.Add(PassbookImage.BackgroundRetina, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/background@2x.png")));
            request.Images.Add(PassbookImage.Logo, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/logo.png")));
            request.Images.Add(PassbookImage.LogoRetina, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/logo@2x.png")));
            request.Images.Add(PassbookImage.Strip, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/strip.png")));
            request.Images.Add(PassbookImage.StripRetina, System.IO.File.ReadAllBytes(Server.MapPath("~/Icons/Cafe/strip@2x.png")));

            request.AuthenticationToken = "vxwxd7J8AlNNFPS8k0a0FfUFtq0ewzfDC";
            RegisterUpdate(request);

            byte[] generatedPass = generator.Generate(request);

            return new FileContentResult(generatedPass, "application/vnd.apple.pkpass") { FileDownloadName = "Pass.pkpass" };

        }

        public ActionResult DoTestAction() {
            Database.PassController.DoTest();
            return null;
        }
    }
}
